<?
$form =& Form::singleton ('payment.xml');

if (!$form->load ($itemId))
	throw new Exception (__('Unable to load the data of the item!'));



if( isset($_GET['target']) && $_GET['target'] != 'print')
{
	$field = $form->getField ('_VALUE_');
	
	$typePgto = $form->getField ('_TYPE_');
		
	$start =  $form->getField ('_START_TIME_')->getValue () ;
	
	try
	{
		$db = Database::singleton ();
				
		$sth = $db->prepare ('SELECT magic.type_payment.value, magic.type_payment.proportional FROM '. $form->getTable (). ' 
							  INNER JOIN magic.type_payment ON magic.type_payment.id = '. $form->getTable (). '.type_payment
							  WHERE '. $form->getTable (). '.'. $form->getPrimary (). ' = ' . $itemId);
				
		$sth->execute ();
		
		$obj = $sth->fetch(PDO::FETCH_OBJ);
	}
	catch (PDOException $e)
	{
		throw new Exception ($e->getMessage ());
	}
				
	if(elapsedTimeMinutes ($start[0].':'. $start[1] .':'.$start[2])/60 <= 20 && $obj->proportional)
		$field->setValue (4);
	else
	{		
		$value = (float) str_replace (array ('$', ','), '', $obj->value);
		
		if($obj->proportional)
			$field->setValue ((float)($value/60)* (elapsedTimeMinutes ($start[0].':'. $start[1] .':'.$start[2])/60));
		else
			$field->setValue ($value);
	}		
	
	$end = date ('H:i:s');
	$end = explode(':', $end);
	$end[0] = $end[0] -1;
	
	$form->getField ('_END_TIME_')->setValue (implode(':', $end));
	
	try
	{
		$db = Database::singleton ();
				
		$sth = $db->prepare ('UPDATE '. $form->getTable (). ' SET _end_date = now() WHERE '. $form->getPrimary (). ' = ' . $itemId);
				
		$sth->execute ();
	}
	catch (PDOException $e)
	{
		throw new Exception ($e->getMessage ());
	}
	
	if (!$form->recovery ())
		throw new Exception (__ ('Unable to retrieve the data submitted!'));
	
	if (!$form->save ($itemId))
		throw new Exception (__ ('Unable to save the data submitted!'));
	

}
?>